﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Xml;
using System.Collections;

namespace BOMS.TcpClientSimulator
{
    public static class Data
    {
        public static string GetServerIP(out int ServerPort)
        {
            string Path = System.IO.Directory.GetCurrentDirectory();
            string xmlPath = System.IO.Path.Combine(Path, "Config.xml");
            XmlDocument doc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;       //忽略注释 
            XmlReader reader = XmlReader.Create(xmlPath, settings);
            doc.Load(reader);
            XmlNode node = doc.SelectSingleNode("/Root/Server");
            if (node != null)
            {
                ServerPort = int.Parse(node["Port"].InnerText.ToString().Trim());
                return node["IP"].InnerText.ToString().Trim();
            }
            ServerPort = 0;
            return "";
           
        }
        public static XmlDocument GetDataConfigXmlDoc()
        {
            string Path = System.IO.Directory.GetCurrentDirectory();
            string xmlPath = System.IO.Path.Combine(Path, "Config.xml");
            XmlDocument doc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;       //忽略注释 
            XmlReader reader = XmlReader.Create(xmlPath, settings);
            doc.Load(reader);
            return doc;
        }
        public static string GetSimCodeFromXml(XmlDocument xDoc ,string Ip)
        {
            XmlNodeList nodes = xDoc.SelectNodes("/Root/SimulateClients/Client");
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Attributes["Ip"].Value.ToString() == Ip)
                {
                    return nodes[i].Attributes["EquipID"].Value.ToString();
                }
            }
            return "11111111111";
        }
        /*
        public static string ToString(this IEnumerable Enumerator, string Seperator, string ElementFormat="{0}")
        {
            StringBuilder sb = new StringBuilder();
            IEnumerator enmor = Enumerator.GetEnumerator();
            int i = 0;
            while (enmor.MoveNext())
            {
                ++i;
                sb.AppendFormat(ElementFormat + Seperator, enmor.Current.ToString());
            }
            if (i > 0)
            {
                sb.Length -= Seperator.Length;
            }
            return sb.ToString();
        } */
        public static string ToString(IEnumerable Enumerator, string Seperator, string ElementFormat = "{0}")
        {
            StringBuilder sb = new StringBuilder();
            IEnumerator enmor = Enumerator.GetEnumerator();
            int i = 0;
            while (enmor.MoveNext())
            {
                ++i;
                sb.AppendFormat(ElementFormat + Seperator, enmor.Current.ToString());
            }
            if (i > 0)
            {
                sb.Length -= Seperator.Length;
            }
            return sb.ToString();
        } 
        public static string byteToHexStr(byte[] bytes, bool IsShorted)
        {
            StringBuilder returnStr = new StringBuilder();
            if (!IsShorted)
            {
                if (bytes != null)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        returnStr.AppendFormat(bytes[i].ToString("X2") + " ");
                    }
                }
            }
            else
            {
                if (bytes.Length > 10)
                {
                    int i;
                    for (i = 0; i < 10; i++)
                    {
                        returnStr.AppendFormat(bytes[i].ToString("X2") + " ");
                    }
                    returnStr.Append("......");
                    for (i = bytes.Length - 10; i < bytes.Length; i++)
                    {
                        returnStr.AppendFormat(bytes[i].ToString("X2") + " ");
                    }
                }
                else
                {

                    for (int i = 0; i < bytes.Length; i++)
                    {
                        returnStr.AppendFormat(bytes[i].ToString("X2") + " ");
                    }
                }
            }
            return returnStr.ToString();
        }
    }
}
